// import { nullFormat } from 'numeral';
import {
  query,
  queryCurrent,
  add,
  edit,
  remove,
  searchRole,
  searchUser,
  checkedUserName,
  active,
  lock,
  batchesRemove,
  searchOrg,
  batchAdd,
  exportDealer,
  getUserDetails
} from '@/services/user2';

export default {
  namespace: 'user2',
  state: {
    data: {
      list: [],
      pagination: {},
    },
    loading: false,
    currentUser: {
      levelCode: '1-2', // 写死
      authLoading: false,
      realName: '',
      userName: '',
      imgUrl: '',
      buttons: [],
      firstLogin: false,
      authInfo: {
        children: [],
      },
      projectCodes: [],
      brandCodes: [],
    },
    customerData: {
      id: null,
      userName: null,
      realName: null,
      customerName: null,
      remark: null
    }
  },
  effects: {
    *fetch({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(query, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    *fetchCurrent({ callback }, { call, put }) {
      yield put({
        type: 'currentSave',
        payload: { authLoading: true },
      });
      const response = yield call(queryCurrent);
      yield put({
        type: 'currentSave',
        payload: { ...response.data, authLoading: false },
      });
      if (callback) callback(response.data && response.data.firstLogin);
    },
    *fetchCurrentNew({ callback }, { call, put }) {
      yield put({
        type: 'currentSave',
        payload: { authLoading: true },
      });
      const response = yield call(queryCurrent);
      yield put({
        type: 'currentSave',
        payload: { ...response.data, authLoading: false },
      });
      if (callback) callback(response.data);
    },
    *checkedUserName({ payload, callback }, { call }) {
      const response = yield call(checkedUserName, payload);
      if (callback) callback(response);
    },
    *add({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(add, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *batchAdd({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(batchAdd, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *remove({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(remove, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *batchesRemove({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      yield call(batchesRemove, payload);
      const response = yield call(query, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *update({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(edit, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *searchRole({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(searchRole, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *fetchUser({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(searchUser, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *searchOrg({ payload, callback }, { call }) {
      const response = yield call(searchOrg, payload);
      if (callback) callback(response);
    },
    *active({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(active, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });

      if (callback) callback(response);
    },
    *lock({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(lock, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *exportDealer({ payload, callback }, { call, put }) {
      yield put({
        type: 'doReducers',
        payload: { loading: true },
      });
      const response = yield call(exportDealer, payload);
      yield put({
        type: 'doReducers',
        payload: { loading: false },
      });
      if (callback) callback(response);
    },
    /**
     * 获取用户详情（包含customerId信息）
     * @param {} param0 
     * @param {*} param1 
     */
     *getUserDetails({ payload, callback }, { call, put }) {
      yield put({
        type: 'customerSave',
        payload: { loading: true },
      });
      const response = yield call(getUserDetails, payload)
      yield put({
        type: 'customerSave',
        payload: { ...response.data, loading: false },
      });
      if (callback) callback(response.data);
    },
  },
  reducers: {
    doReducers(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    currentSave(state, action) {
      const { currentUser } = state;
      const current = { ...currentUser, ...action.payload };
      return {
        ...state,
        ...{ currentUser: current },
      };
    },
    customerSave(state, action) {
      console.log(state)
      const { customerData } = state;
      const current = { ...customerData, ...action.payload };
      return {
        ...state,
        ...{ customerData: current },
      };
    },
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
    changeAuthLoading(state, action) {
      return {
        ...state,
        authLoading: action.payload,
      };
    },
    saveCurrentUser(state, action) {
      return {
        ...state,
        currentUser: action.payload,
      };
    },
    changeNotifyCount(state, action) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
          notifyCount: action.payload,
        },
      };
    },
  },
};
